﻿// ———————————————————————–
// <copyright file="mFormDistRef.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// MessageManager.cs - Class To Handle I/O Operations For Message
// Project: IC.NET - EDXL Test App
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010 Beta 2
// Author:      Joseph Whitmore MITRE Bedfor
//              jwhitmore@mitre.org
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EDXLSharp.EDXLTestApplication
{
  /// <summary>
  /// Form to build/edit a distribution block of EDXL message
  /// </summary>
  public partial class MFormDistRef : Form
  {
    /// <summary>
    /// EDXL Message Distribution ID
    /// </summary>
    private string distributionID = null;

    /// <summary>
    /// EDXL Message Sender ID
    /// </summary>
    private string senderID = null;

    /// <summary>
   /// EDXL Message Date and Time that message is sent.
   /// </summary>
    private DateTime dateTimeSent;

    /// <summary>
    /// Initializes a new instance of the MFormDistRef class
    /// </summary>
    public MFormDistRef()
    {
      this.InitializeComponent();
    }

    /// <summary>
    /// Initializes a new instance of the MFormDistRef class
    /// </summary>
    /// <param name="str">Comma delimited string of a valid EDXL Distribution reference</param>
    public MFormDistRef(string str)
    {
      this.InitializeComponent();

      // parse string
      if (str != null)
      {
        string[] vals = str.Split(',');
        try
        {
          this.mTextBoxDistID.Text = vals[0];
          this.mTextBoxSenderID.Text = vals[1];
          this.mTextBoxDateTimeSent.Text = vals[2];
        }
        catch (Exception ex)
        {
          ex.ToString();
        }
      }
    }

    /// <summary>
    /// Gets or sets
    /// EDSL Message Distribution ID
    /// </summary>
    public string DistibutionID
    {
      get
      {
        return this.distributionID;
      }

      set
      {
        this.distributionID = value;
      }
    }

    /// <summary>
    /// Gets or sets
    /// EDXL Message Sender ID
    /// </summary>
    public string SenderID
    {
      get
      {
        return this.senderID;
      }

      set
      {
        this.senderID = value;
      }
    }
    
    /// <summary>
    /// Gets or sets
    /// EDXL Message DateTime
    /// </summary>
    public DateTime DateTimeSent
    {
      get
      {
        return this.dateTimeSent;
      }

      set
      {
        this.dateTimeSent = value;
      }
    }

    /// <summary>
    /// Creates the comma delimited Distribution
    /// </summary>
    /// <returns>Comma delimited EDXL Distribution Reference string</returns>
    public string MGetDistRefString()
    {
      if (this.distributionID != null && this.distributionID.Length > 0)
      {
        if (this.senderID != null && this.senderID.Length > 0)
        {
          if (this.dateTimeSent != null)
          {
            return this.distributionID + "," + this.senderID + "," + this.dateTimeSent.ToString("o");
          }
        }
      }

      return null;
    }

    /// <summary>
    /// Close form and make no changes
    /// </summary>
    /// <param name="sender">Object generating the event</param>
    /// <param name="e">Event generated by the object</param>
    private void MButtonCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    /// <summary>
    /// Close form and make changes available to the parent form
    /// </summary>
    /// <param name="sender">Object generating the event</param>
    /// <param name="e">Event generated by the object</param>
    private void MButtonCommit_Click(object sender, EventArgs e)
    {
      this.distributionID = this.mTextBoxDistID.Text;
      this.senderID = this.mTextBoxSenderID.Text;
      try
      {
        this.dateTimeSent = DateTime.Parse(this.mTextBoxDateTimeSent.Text);
      }
      catch (ArgumentException ex)
      {
        ex.ToString();
      }

      this.Close();
    }
  }
}
